# 下载群文件里的 access.log-20191117
# 通过正则表达式匹配，找出文件中所有访问记录里
# 访问次数最多的前 10 个 IP，访问最频繁的前 10 个页面

import re

pattern = '''(?P<remote_ip>[\d.]{7,}) - - \[[\w/: +]+\] "\w+ (?P<access_url>\S+) .+"'''

regex = re.compile(pattern)

origin_remote_ip = {}
origin_access_url = {}

def log_data_statistics(log_file):
    with open(log_file) as f:
        for line in f:
            match = regex.match(line)
            if match:
                remote_ip = match.group('remote_ip')
                access_url = match.group('access_url')

            origin_remote_ip[remote_ip] = origin_remote_ip.get(remote_ip, 0) + 1
            origin_access_url[access_url] = origin_access_url.get(access_url, 0) + 1

    sort_remote_ip = sorted(origin_remote_ip.keys(),key=lambda x:origin_remote_ip[x],reverse=True)
    sort_access_url = sorted(origin_access_url.keys(),key=lambda x:origin_access_url[x],reverse=True)

    print("Top Ten Of IP Access:{}".format(sort_remote_ip[:10]))
    print("Top Ten Of URL Access:{}".format(sort_access_url[:10]))

log_data_statistics("access.log-20191119")

# 运行结果：
Top Ten Of IP Access:['27.17.18.210', '182.254.52.17', '101.89.19.140', '61.241.50.63', '59.36.132.240', '14.18.182.223', '183.192.179.16', '66.249.64.15', '122.51.99.115', '66.249.64.17']
Top Ten Of URL Access:['/', '/test.php', '/1.php', '/robots.txt', '/shell.php', '/qq.php', '/index.php', '/x.php', '/cmd.php', '/manager/html']

# IP匹配结果是对的，URL匹配中对于一些后面带参数的页面的情况没有考虑到。
# 例如/index.php?a=1&b=2这个页面和/index.php是相同的页面，但是都被你的表达式忽略了。